import 'dart:core';

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_bsbdj/bean/music_persional.dart';
import 'package:flutter_bsbdj/pagers/music/music_play_detail.dart';
import 'package:flutter_bsbdj/pagers/music/music_suspension_window.dart';

import 'constant.dart';

class WidgetViews {
  static WidgetViews widgetViews;

  static WidgetViews getInstance() {
    if (widgetViews == null) {
      widgetViews = WidgetViews();
    }
    return widgetViews;
  }

  //返回带错误提示的image
  Widget cachedImage(String url, {Function function, Function longPress}) {
    return GestureDetector(
      child: CachedNetworkImage(
        imageUrl: url,
        placeholder: (context, str) => Image.asset("images/ic_loading.gif"),
        errorWidget: (context, str, object) => Image.asset("images/error.png"),
      ),
      onTap: () {
        if (function != null) {
          function();
        }
      },
      onLongPress: () {
        if (longPress != null) {
          longPress();
        }
      },
    );
  }

  //
  Widget cachedImageCover(String url, {Function function, Function longPress}) {
    return GestureDetector(
      child: CachedNetworkImage(
        imageUrl: url,
        fit: BoxFit.cover,
        placeholder: (context, str) => Image.asset("images/ic_loading.gif"),
        errorWidget: (context, str, object) => Image.asset("images/error.png"),
      ),
      onTap: () {
        if (function != null) {
          function();
        }
      },
      onLongPress: () {
        if (longPress != null) {
          longPress();
        }
      },
    );
  }

  Widget loading() {
    return Container(
        alignment: Alignment.center,
        child: Container(
          width: 30,
          height: 30,
          margin: EdgeInsets.all(5),
          child: CircularProgressIndicator(),
        ));
  }

  Widget nullDataPormat(Function function) {
    return GestureDetector(
      child: Center(
        child: Text("未获取到数据，点击刷新"),
      ),
      onTap: function,
    );
  }

  //当音乐播放时，在页面最下方显示播放信息
  Widget musicPlayInfo(
      String header,
      double progress,
      String position,
      String duration,
      double width,
      double height,
      bool playing,
      Function stateChange,
      BuildContext context,
      PersonalItem item) {
    return Container(
      height: height,
      width: width,
      decoration: BoxDecoration(
        color: Color(0xFFFFFFFF),
      ),
      child: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(left: 10),
            width: 50,
            height: 50,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(25))),
            child: ClipOval(
              child:
                  WidgetViews.getInstance().cachedImage(header, function: () {
                MusicWindowPlay.getInstance(context).isHeidden(true);
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => MusicDetail(item))).then((value){
                      if(MusicWindowPlay.getInstance(context).playing) {
                        MusicWindowPlay.getInstance(context).isHeidden(false);
                      }
                });
              }),
            ),
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.all(5),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    height: 3,
                    margin: EdgeInsets.only(top: 12),
                    child: LinearProgressIndicator(
                      value: progress,
                      valueColor: AlwaysStoppedAnimation(Colors.blueAccent),
                      backgroundColor: Colors.grey,
                    ),
                  ),
                  Container(
                    child: Expanded(
                        child: Row(
                      children: <Widget>[
                        Container(
                          padding: EdgeInsets.only(bottom: 5),
                          child: Text(
                            "$position / $duration",
                            style: TextStyle(
                                fontSize: 10,
                                color: Colors.black54,
                                decoration: TextDecoration.none),
                          ),
                          alignment: Alignment.bottomLeft,
                        ),
                        Expanded(
                          child: Container(
                            width: 40,
                            height: 40,
                            padding: EdgeInsets.only(right: 15),
                            child: GestureDetector(
                              child: Icon(
                                playing ? Icons.pause : Icons.play_arrow,
                                color: Colors.blueAccent,
                                size: 40,
                              ),
                              onTap: stateChange,
                            ),
                            alignment: Alignment.bottomRight,
                          ),
                        ),
                      ],
                    )),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  static Widget themeWidget(Widget widget){
    return MaterialApp(
      theme: Constant.getInstance().getTheme()?ThemeData.light():ThemeData.dark(),
      home: widget,
    );
  }
}
